package org.cloudie.docs.editor.client.parts;

import org.cloudie.docs.editor.client.commands.Command;
import org.cloudie.docs.editor.client.commands.generic.BrowserOpenCommand;
import org.cloudie.docs.editor.client.commands.generic.CurrentDocumentMakeHomepageCommand;
import org.cloudie.docs.editor.client.commands.generic.CurrentDocumentSaveCommand;
import org.cloudie.docs.editor.client.commands.generic.DesktopFullScreenCommand;
import org.cloudie.docs.editor.client.commands.generic.DialogFolderNewCommand;
import org.cloudie.docs.editor.client.commands.generic.DialogIconNewCommand;
import org.cloudie.docs.editor.client.commands.generic.DialogPreferencesCommand;
import org.cloudie.docs.editor.client.commands.generic.DialogWindowNewCommand;
import org.cloudie.docs.editor.client.commands.generic.ExistingDocumentOpenCommand;
import org.cloudie.docs.editor.client.commands.generic.NewDocumentStartCommand;
import org.cloudie.docs.editor.client.events.CommandEvent;
import org.cloudie.docs.editor.client.resources.icons.IconsHelper;
import org.cloudie.docs.editor.client.widgets.SearchBox;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.http.client.URL;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.ToggleButton;
import com.google.gwt.user.client.ui.Widget;

/**
 * A specialized, non-reusable widget containing the main toolbar.
 */
public class ToolbarPart extends Composite {

  private FlexTable bar;
  private SearchBox box;
  
  /**
   * Constructs a Toolbar part.
   */
  public ToolbarPart() {
	box = new SearchBox(IconsHelper.icons.Google());
	box.setWidth("250px");
	box.getButton().addClickHandler(new ClickHandler() {
        @Override
		public void onClick(ClickEvent event) {
          CommandEvent.fire(new BrowserOpenCommand("http://www.google.com/#q=" +
              URL.encodeComponent(box.getTextBox().getText()), "gsearch")); 
		}
	});
	box.getTextBox().addKeyPressHandler(new KeyPressHandler() {
		@Override
		public void onKeyPress(KeyPressEvent event) {
		  if (event.getCharCode() == KeyCodes.KEY_ENTER) {
            CommandEvent.fire(new BrowserOpenCommand("http://www.google.com/#q=" +
                URL.encodeComponent(box.getTextBox().getText()), "gsearch")); 
		  }
		}
	});
	bar = new FlexTable();
	bar.setBorderWidth(0);
	bar.setCellPadding(0);
	bar.setCellSpacing(0);
	bar.insertRow(0);
	bar.insertCell(0, 0);
	bar.insertCell(0, 1);
	bar.getFlexCellFormatter().setHorizontalAlignment(0, 0, HorizontalPanel.ALIGN_LEFT);
	bar.getFlexCellFormatter().setHorizontalAlignment(0, 1, HorizontalPanel.ALIGN_RIGHT);
	bar.setHeight("30px");
	bar.setWidth("100%");
	bar.setStylePrimaryName("cloudie-Tools-Panel");
	bar.setWidget(0, 0, buildToolBar());
	bar.setWidget(0, 1, box);
    initWidget(bar);
  }
  
  /**
   * Builds the main toolbar.
   * 
   * @return the main toolbar
   */
  private HorizontalPanel buildToolBar() {
    HorizontalPanel toolbarPanel = new HorizontalPanel();
    toolbarPanel.setVerticalAlignment(HorizontalPanel.ALIGN_MIDDLE);
    toolbarPanel.setStyleName("cloudie-Toolbar");
    toolbarPanel.add(buildButton(IconsHelper.icons.FileNew(), "New Desktop", false, new NewDocumentStartCommand()));
    toolbarPanel.add(buildButton(IconsHelper.icons.OpenDocument(), "Open Desktop", false, new ExistingDocumentOpenCommand()));
    toolbarPanel.add(buildButton(IconsHelper.icons.Save(), "Save Desktop", false, new CurrentDocumentSaveCommand()));
    toolbarPanel.add(buildSeparator());
    toolbarPanel.add(buildButton(IconsHelper.icons.FullScreen(), "Toggle Full Screen", false, new DesktopFullScreenCommand()));
    toolbarPanel.add(buildSeparator());
    toolbarPanel.add(buildButton(IconsHelper.icons.Style(), "Desktop Preferences", false, new DialogPreferencesCommand()));
    toolbarPanel.add(buildButton(IconsHelper.icons.IconNew(), "New Icon", false, new DialogIconNewCommand()));
    toolbarPanel.add(buildButton(IconsHelper.icons.FolderNew(), "New Folder", false, new DialogFolderNewCommand()));
    toolbarPanel.add(buildButton(IconsHelper.icons.WindowNew(), "New Window", false, new DialogWindowNewCommand()));
    toolbarPanel.add(buildSeparator());
    toolbarPanel.add(buildButton(IconsHelper.icons.Home(), "Make this desktop your homepage.", false, new CurrentDocumentMakeHomepageCommand()));
    return toolbarPanel;
  }
  
  /**
   * Builds a toolbar button.
   * 
   * @param icon the button's icon
   * @param title the button's tooltip text
   * @param isToggle whether the button is of type toggle or push
   * @param command the command type for the toolbar button
   * @return a toolbar button
   */
  private Widget buildButton(AbstractImagePrototype icon, String title, boolean isToggle, final Command command){
    if(isToggle){
      final ToggleButton btn = new ToggleButton(icon.createImage());
      btn.setTitle(title);
      btn.addClickHandler(new ClickHandler(){
        public void onClick(ClickEvent event) {
          btn.setFocus(false);
          btn.removeStyleName("gwt-ToggleButton-up-hovering");
          CommandEvent.fire(command);
        }
      });
      return btn;
    }else{
      final PushButton btn = new PushButton(icon.createImage());
      btn.setTitle(title);
      btn.addClickHandler(new ClickHandler(){
        public void onClick(ClickEvent event) {
          btn.setFocus(false);
          btn.removeStyleName("gwt-PushButton-up-hovering");
          CommandEvent.fire(command);
        }
      });
      return btn;
    }
  }
  
  private Image buildSeparator() {
	Image sep = IconsHelper.icons.Separator().createImage();
	sep.addStyleName("separator");
    return sep;
  }

}
